package com.example.service.impl;

import com.example.mapper.TeacherSecondPageMapper;
import com.example.pojo.*;
import com.example.service.TeacherSecondPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class TeacherSecondPageImpl implements TeacherSecondPageService {

    @Autowired
    private TeacherSecondPageMapper teacherSecondPageMapper;

    @Override
    public List<Student> loadScores(Integer teacherId) {
        List<Student> studentList = teacherSecondPageMapper.loadScores(teacherId);
        return studentList;
    }

    @Override
    public Result editScore(Score score) {
        try {
            if(!(score.getNewSubjectName().equals(score.getSubjectName())) &&teacherSecondPageMapper.isExistScore(score)){
                return Result.fail("该学生已存在该课程成绩");
            }
            Percentage  percentage= teacherSecondPageMapper.inqueryPercentage(score);
            //计算综合分数
            String percentageUsualScore = percentage.getPercentageUsualScore().replaceAll("%", "");
            String percentageMidtermScore = percentage.getPercentageMidtermScore().replaceAll("%", "");
            String percentageFinalScore = percentage.getPercentageFinalScore().replaceAll("%", "");
            //计算综合分数
            score.setScore((score.getUsualScore().multiply(new BigDecimal(percentageUsualScore)).divide(new BigDecimal(100))).add(score.getMidtermScore().multiply(new BigDecimal(percentageMidtermScore)).divide(new BigDecimal(100))).add(score.getFinalScore().multiply(new BigDecimal(percentageFinalScore)).divide(new BigDecimal(100))));

            if (score.getScore().compareTo(new BigDecimal(60)) >= 0) {
                Integer credit = teacherSecondPageMapper.inqueryCredit(score);
                score.setEarnCredit(credit);
            } else {
                score.setEarnCredit(0);
            }
            teacherSecondPageMapper.editScore(score);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.fail("修改失败:" + e.getMessage());
        }
    }

    @Override
    public Result deleteScore(Score score) {
        try {
            teacherSecondPageMapper.deleteScore(score);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.fail("删除失败:" + e.getMessage());
        }
    }

}
